import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { listDept, getDept, delDept, addDept, updateDept } from '@/api/system/dept'

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'deptId'
  const moduleName =  '部门'
  const apiFn = {
    listApiFn: listDept,
    delApiFn: delDept,
    apiFnConfig:{
      info: getDept,
      add: addDept,
      edit: updateDept
    }
  }

  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  /** 查询菜单下拉树结构 */
  const menuOptions = ref([])
  function getTreeSelect() {
    menuOptions.value = [];
    listMenu().then(response => {
      const menu = { menuId: 0, menuName: "主类目", children: [] };
      menu.children = proxy.handleTree(response.data, "menuId");
      menuOptions.value.push(menu);
    });
  }

  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
    getTreeSelect,
    menuOptions
  }

}


/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    "sys_normal_disable",
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: '上级部门',
      prop: 'parentId',
      hideInSearch: true,
      hideInTable:true,
      hideInForm: computed(()=> formData.value.parentId === 0) ,
      colProps: {
        span: 24
      }
    },
    {
      label: '部门名称',
      prop: 'deptName',
      tableColumnProps: {
        align: 'center'
      },
      order: 10
    },
    {
      label: '显示排序',
      prop: 'orderNum',
      hideInSearch: true,
      tableColumnProps: {
        align: 'center'
      },
      propInForm:{
        valueType:'input-number',
        filedProps:{
          min:0,
        }
      },
      order: 9
    },
    {
      label: '负责人',
      prop: 'leader',
      hideInSearch: true,
      hideInTable: true,
    },
    {
      label: '联系电话',
      prop: 'phone',
      hideInSearch: true,
      hideInTable: true,
    },
    {
      label: '邮箱',
      prop: 'email',
      hideInSearch: true,
      hideInTable: true,
    },
    {
      label: '状态',
      prop: 'status',
      tableColumnProps: {
        align: 'center'
      },
      valueType: 'select',
      options: dictRawData.sys_normal_disable,
      order: 8,
      propInForm: {
        valueType: 'radio'
      }
    },
    {
      label: '创建时间',
      prop: 'createTime',
      tableColumnProps: {
        align: 'center'
      },
      hideInSearch: true,
      hideInForm:true,
      valueType: 'date-picker',
      fieldProps: {
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD'
      }
    },
    {
      label: '操作',
      prop: 'action',
      width: '240px',
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: 'center',
        fixed: 'right'
      }
    }
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

